/*
* $Id: ViewFamilies.cpp 124 2010-07-26 14:12:13Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ViewFamilies.h"
#include "Database/Tables/FamiliesTable.h"
#include "Database/Operation/LastInspectOperation.h"
#include "Settings/Journal/FamilyViewSettings.h"
#include "Settings/DinIntDefined.h"
#include "Settings/SettingControlIds.h"
#include "Components/Journal/JournalInterface.h"
#include "Components/Journal/QueenBeeManager.h"
#include "ViewFieldValuesHelper.h"
#include "Event/EventId.h"

CViewFamilies::CViewFamilies(CState* pState)
: CViewTable(pState){
	IApplication::GetApp()->GetEventManager()->SubscribeEvent(this, FAMILY_STN_CHANGE_EVENT);
	IApplication::GetApp()->GetEventManager()->SubscribeEvent(this, QUEEN_BEE_DB_CHANGE_EVENT);
	IApplication::GetApp()->GetEventManager()->SubscribeEvent(this, JOURNAL_DB_CHANGE_EVENT);
}
CViewFamilies::~CViewFamilies(){
	IApplication::GetApp()->GetEventManager()->UnSubscribeEvent(this, FAMILY_STN_CHANGE_EVENT);
	IApplication::GetApp()->GetEventManager()->UnSubscribeEvent(this, QUEEN_BEE_DB_CHANGE_EVENT);
	IApplication::GetApp()->GetEventManager()->UnSubscribeEvent(this, JOURNAL_DB_CHANGE_EVENT);
}

bool CViewFamilies::IsViewState(State state){
	return state.m_nPosTab == JournalEnum::INSPECT_JOURNAL &&
		state.m_type == JournalEnum::NONE &&
		state.m_nTypeSel == JournalEnum::POINT;
}

void CViewFamilies::SetState(){
	m_pState->m_nState = JournalEnum::VIEW_FAMILY;
	Init();
}

void CViewFamilies::Init(){
	m_tableRegion.GetTableDrawer()->ResetTable();
	tableHeader *nameHeader = NULL, *typeHeader = NULL, *noteHeader = NULL;
	nameHeader = m_tableRegion.GetTableDrawer()->AddHeader(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_FAMILY_NAME_ID), TABLE_FAMILY_NAME_ID, tableHeader::STRING);
	CFamilyViewSettings pSettings;
	if(pSettings.GetState(CHECK_TYPE_ID) == CHECK_BOX_STATE_1)
		typeHeader = m_tableRegion.GetTableDrawer()->AddHeader(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_FAMILY_TYPE_ID), TABLE_FAMILY_TYPE_ID, tableHeader::STRING);
	if(pSettings.GetState(CHECK_NOTE_ID) == CHECK_BOX_STATE_1)
		noteHeader = m_tableRegion.GetTableDrawer()->AddHeader(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), TABLE_FAMILY_NOTE_ID, tableHeader::STRING);
	CViewFieldValuesHelper helper(m_tableRegion.GetTableDrawer());
	helper.GenerateUserHeaders(CViewFieldValuesHelper::FAMILIES, TABLE_FAMILY_USER_ID);
	tableHeader *strengthHeader = NULL, *isQueenBeeHeader = NULL, *honeyHeader = NULL,	*broodHeader = NULL,
		*housingHeader = NULL, *woxHeader = NULL, *frameHeader = NULL, *birthdayQueenBeeHeader = NULL;
	if(pSettings.GetState(CHECK_FRAME_ID) == CHECK_BOX_STATE_1)
		frameHeader = m_tableRegion.GetTableDrawer()->AddHeader(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_FRAME_ID), TABLE_FAMILY_FRAME_ID, tableHeader::INTEGER);
	if(pSettings.GetState(CHECK_BEES_ID) == CHECK_BOX_STATE_1)
		strengthHeader= m_tableRegion.GetTableDrawer()->AddHeader(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_BEES_ID), TABLE_FAMILY_BEES_ID, tableHeader::INTEGER);
	if(pSettings.GetState(CHECK_HONEY_ID) == CHECK_BOX_STATE_1)
		honeyHeader = m_tableRegion.GetTableDrawer()->AddHeader(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_HONEY_ID), TABLE_FAMILY_HONEY_ID, tableHeader::INTEGER);
	if(pSettings.GetState(CHECK_BROOD_ID) == CHECK_BOX_STATE_1)
		broodHeader = m_tableRegion.GetTableDrawer()->AddHeader(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_BROOD_ID), TABLE_FAMILY_BROOD_ID, tableHeader::INTEGER);
	if(pSettings.GetState(CHECK_WOX_ID) == CHECK_BOX_STATE_1)
		woxHeader = m_tableRegion.GetTableDrawer()->AddHeader(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_WAX_ID), TABLE_FAMILY_WOX_ID, tableHeader::INTEGER);
	if(pSettings.GetState(CHECK_HOUSING_ID) == CHECK_BOX_STATE_1)
		housingHeader = m_tableRegion.GetTableDrawer()->AddHeader(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_HOUSING_ID), TABLE_FAMILY_HOUSING_ID, tableHeader::INTEGER);
	if(pSettings.GetState(CHECK_EXIST_BEE_QUEEN_ID) == CHECK_BOX_STATE_1)
		isQueenBeeHeader = m_tableRegion.GetTableDrawer()->AddHeader(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_EBQ_ID), TABLE_FAMILY_EBQ_ID, tableHeader::BOOL);
	if(pSettings.GetState(CHECK_BIRTHDAY_BEE_QUEEN_ID) == CHECK_BOX_STATE_1)
		birthdayQueenBeeHeader = m_tableRegion.GetTableDrawer()->AddHeader(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_BBQ_ID), TABLE_FAMILY_BBQ_ID, tableHeader::DATE);
	m_tableRegion.GetTableDrawer()->NormalizeHeader();

	IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CFamiliesTable families;
	families.m_content.s_nIdPoint = m_pState->m_nIdParameter;
	IResult* result = families.GetValue(pDb);
	for(int i = 0; i < (int)result->GetSizeResult(); i++){
		CFamiliesTable::content* family = (CFamiliesTable::content*)(*result)[i];
		CLastInspectOperation operation;
		operation.m_content.s_nIdFamily = family->s_nId;
		IResult* res = operation.GetValue(pDb);
		CTableCelRow *row = new CTableCelRow(family->s_nId, JournalEnum::VIEW_FAMILY);
		row->AddCel(nameHeader, family->s_strName);
		if(noteHeader)
			row->AddCel(noteHeader, family->s_strNote);
		if(typeHeader)
			row->AddCel(typeHeader, families.GetStringType(family->s_type));
		std::tstring strFrame, strStrength, strHoney, strBrood, strWox, strHousing, strIsQueenBee;
		if(res->GetSizeResult()){
			CLastInspectOperation::content* pContent = (CLastInspectOperation::content*)(*res)[0];
			strFrame = CConvertFunction::ConvertFromInt(pContent->s_nFrame);
			strHoney = CConvertFunction::ConvertFromInt(pContent->s_nHoney);
			strBrood = CConvertFunction::ConvertFromInt(pContent->s_nBrood);
			strStrength = CConvertFunction::ConvertFromInt(pContent->s_nStrength);
			strWox = CConvertFunction::ConvertFromInt(pContent->s_nWax);
			strHousing = CConvertFunction::ConvertFromInt(pContent->s_nHousing);
			strIsQueenBee = IApplication::GetApp()->GetLanguageManager()->GetString(QUEEN_BEE_STATUS_DEFAULT_ID + pContent->s_bQueenBee);
		}
		std::tstring strBirthDayQB;
		std::map<int, CQueenBeeManager::queen_bee> aQb = IJournalComponent::GetJournalComponent()->GetQueenBeeManager()->GetFamilyQB(family->s_nId);
		for(std::map<int, CQueenBeeManager::queen_bee>::iterator it = aQb.begin(); it != aQb.end(); it++){
			if(!it->second.s_bDead)
				strBirthDayQB += CConvertFunction::ConvertFromDate(it->second.s_date);
		}
		if(frameHeader)
			row->AddCel(frameHeader, strFrame);
		if(strengthHeader)
			row->AddCel(strengthHeader, strStrength);
		if(honeyHeader)
			row->AddCel(honeyHeader, strHoney);
		if(broodHeader)
			row->AddCel(broodHeader, strBrood);
		if(woxHeader)
			row->AddCel(woxHeader, strWox);
		if(housingHeader)
			row->AddCel(housingHeader, strHousing);
		if(isQueenBeeHeader)
			row->AddCel(isQueenBeeHeader, strIsQueenBee);
		if(birthdayQueenBeeHeader)
			row->AddCel(birthdayQueenBeeHeader, strBirthDayQB);
		res->Free();
		helper.GenerateUserCels(row, family->s_nIdRecord);
		m_tableRegion.GetTableDrawer()->AddCelRow(row);
	}
	result->Free();
	m_tableRegion.GetTableDrawer()->SortColumn(TABLE_FAMILY_NAME_ID);
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
}

void CViewFamilies::SetManipulation(JournalEnum::typeItemMnpl type, int nId){
	IWnd* pWnd = IApplication::GetApp()->GetVisualManager()->
		GetVisualComponent(IApplication::GetApp()->
		GetLanguageManager()->GetString(JOURNAL_CAPTION_ID))->GetComponentFrame();
	pWnd->Post(UM_ITEM_MANIPULATION, type, nId);
}

BEGIN_MESSAGE_MAP(CViewFamilies, CViewTable)
	ON_COMMAND(BUTTON_ADD_ID, OnAddFamily)
	ON_COMMAND(BUTTON_DELETE_ID, OnDeleteFamily)
	ON_COMMAND(BUTTON_CHANGE_ID, OnChangeFamily)
END_MESSAGE_MAP()

void CViewFamilies::OnAddFamily(){
	SetManipulation(JournalEnum::ADD_FAMILY, m_pState->m_nIdParameter);
}
void CViewFamilies::OnDeleteFamily(){
	CTableCelRow* pSelectRow = m_tableRegion.GetTableDrawer()->GetSelectRow();
	std::tstring strContent = IApplication::GetApp()->GetLanguageManager()->GetString(QUESTION_DELETE_ID) +
		_T(" '") + pSelectRow->GetValueColumn(TABLE_FAMILY_NAME_ID) + _T("'.");
	if(MessageBox(strContent.c_str(), IApplication::GetApp()->GetLanguageManager()->
		GetString(DELETE_FAMILY_ID).c_str(), MB_YESNO) == IDYES){
			SetManipulation(JournalEnum::DELETE_FAMILY, pSelectRow->GetId());
			m_tableRegion.GetTableDrawer()->DeleteRow(pSelectRow);
			Invalidate(FALSE);
	}
}
void CViewFamilies::OnChangeFamily(){
	SetManipulation(JournalEnum::CHANGE_FAMILY, m_tableRegion.GetTableDrawer()->GetSelectRow()->GetId());
}

void CViewFamilies::EventHandler(){
	Init();
}
